package pers.vic.boot.generator.autoconfigure;

import java.util.Collection;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.Assert;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import pers.vic.boot.generator.controller.GeneratorController;
import pers.vic.boot.generator.dao.GeneratorDao;
import pers.vic.boot.generator.dao.GeneratorDao.DatabaseType;
import pers.vic.boot.generator.dao.MysqlGeneratorDao;
import pers.vic.boot.generator.dao.OracleGeneratorDao;
import pers.vic.boot.generator.service.GeneratorService;

/**
 * @description: 代码生成starter 入口
 * @author: Vic.xu
 * @date: 2020年3月10日 上午10:54:39
 */
@Configuration
@ConditionalOnClass(value = { JdbcTemplate.class })
@ConditionalOnWebApplication
@ConditionalOnExpression("${generatir.enabled:true}")
@EnableConfigurationProperties({GeneratorProperties.class})
public class GeneratorAutoconfigigure {

	/**
	 * 本项目中支持的各个数据库类型
	 */
	@Deprecated
	@Autowired
	private Collection<GeneratorDao> generatorDaoList;
	
	@Autowired
	private GeneratorProperties generatorProperties;
	
	@Autowired
	private ApplicationContext applicationContext;
	
	/**
	 * 当前配置的数据库类型 默认MYSQL
	 */
	@Value("${generator.database:mysql}")
	private String database;
	
	/**
	 * 把static HTML所在的文件夹设置为静态资源
	 *  @description: 
	 *  @author Vic.xu
	 *  @date: 2020年5月15日下午5:33:38
	 */
	@Configuration
	static class StaticWebMvcConfigurerAdapter implements  WebMvcConfigurer {
		@Override
	    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//	        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
	    }
	}

	
	
	@Bean
	@Primary
	@ConditionalOnMissingBean
	public MysqlGeneratorDao msqlGeneratorDao(JdbcTemplate jdbcTemplate) {
		return new MysqlGeneratorDao(jdbcTemplate);
	}
	
	@Bean
	@ConditionalOnMissingBean
	public OracleGeneratorDao oracleGeneratorDao(JdbcTemplate jdbcTemplate) {
		return new OracleGeneratorDao(jdbcTemplate);
	}
	
	public GeneratorDao getGeneratorDao() {
		Map<String, GeneratorDao> daoMap = applicationContext.getBeansOfType(GeneratorDao.class);
		DatabaseType type = DatabaseType.getByName(generatorProperties.getDatabase());
		Assert.notNull(type, "当前项目未支持" + database + "类型的数据库代码生成");
		
		GeneratorDao generatorDao = null;
		for(Map.Entry<String, GeneratorDao> entry : daoMap.entrySet()) {
			GeneratorDao dao = entry.getValue();
			if(type == dao.databaseType() ) {
				generatorDao = dao;
				break;
			}
		}
		Assert.notNull(generatorDao, "当前项目未实现" + database + "类型的数据库的GeneratorDao");
		
		return generatorDao;
	}
	
	
	@Bean
	@ConditionalOnMissingBean
	public GeneratorService generatorService(GeneratorProperties generatorProperties) {
		return new GeneratorService(getGeneratorDao(), generatorProperties);
	}
	
	@Bean
	@ConditionalOnMissingBean
	public GeneratorController generatorController(GeneratorService generatorService) {
		return new GeneratorController(generatorService);
	}

}
